package com.qys.livesMall.system.controller.sys;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.framework.view.BaseQuery;
import com.qys.livesMall.system.modal.vo.HomeBannerTypeVO;
import com.qys.livesMall.system.model.dto.sys.H2AppHomeBannerDTO;
import com.qys.livesMall.system.model.dto.sys.H2AppHomeBannerStateDTO;
import com.qys.livesMall.system.model.entity.H2AppHomeBanner;
import com.qys.livesMall.system.service.IH2AppHomeBannerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * <p>
 * 首页轮播图 前端控制器
 * </p>
 *
 * @author dp
 * @since 2023-05-02
 */
@RestController
@RequestMapping("/sys/app/home/banner")
@Tag(name = "H2AppHomeBannerController", description = "后台管理-首页轮播图")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class H2AppHomeBannerController {

    private final IH2AppHomeBannerService homeBannerService;

    @PostMapping("/page")
    @Operation(description = "分页查询轮播图信息")
    public R<Page<H2AppHomeBanner>> page(@RequestBody BaseQuery baseQuery) {
        return R.ok(homeBannerService.page(baseQuery));
    }

    @PostMapping("/add")
    @Operation(description = "新增轮播图信息")
    public R<Void> add(@RequestBody @Validated H2AppHomeBannerDTO dto) {
        H2AppHomeBanner h2AppHomeBanner = new H2AppHomeBanner();
        BeanUtils.copyProperties(dto,h2AppHomeBanner);
        h2AppHomeBanner.setCreateTime(LocalDateTime.now());
        h2AppHomeBanner.setCreateBy(SecurityUtils.getUsername());
        homeBannerService.save(h2AppHomeBanner);
        return R.ok();
    }

    @PutMapping("/edit")
    @Operation(description = "编辑轮播图信息")
    public R<Void> edit(@RequestBody @Validated H2AppHomeBannerDTO dto) {
        H2AppHomeBanner h2AppHomeBanner = new H2AppHomeBanner();
        BeanUtils.copyProperties(dto,h2AppHomeBanner);
        h2AppHomeBanner.setUpdateTime(LocalDateTime.now());
        h2AppHomeBanner.setUpdateBy(SecurityUtils.getUsername());
        homeBannerService.updateById(h2AppHomeBanner);
        return R.ok();
    }

    @DeleteMapping("/{id}")
    @Operation(description = "根据id删除轮播图信息")
    public R<Void> removeById(@PathVariable String id) {
        homeBannerService.removeById(id);
        return R.ok();
    }

    private LocalDateTime longToLocalDateTime(Long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zoneId = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDateTime();
    }

    @PostMapping("/updateState")
    @Operation(description = "修改轮播图状态")
    public R<Void> updateState(@RequestBody @Validated H2AppHomeBannerStateDTO dto) {
        homeBannerService.updateState(dto);
        return R.ok();
    }

    @PostMapping("/typePage")
    @Operation(description = "分页查询轮播图分类数据信息")
    public R<Page<HomeBannerTypeVO>> typePage(@RequestBody BaseQuery baseQuery) {
        return R.ok(homeBannerService.typePage(baseQuery));
    }
}
